const appid = document.querySelector("input#appid");
const token = document.querySelector("input#token");
const dialog = document.querySelector("dialog#setting-dialog");

function updateCfg() {
  appid.value = localStorage.getItem("appid");
  token.value = localStorage.getItem("token");
  if (!appid.value || !token.value) dialog.showModal();
  tts
    .updateCfg({
      appid: appid.value,
      token: token.value,
    })
    .catch(alert);
}
updateCfg();

document.querySelector("button#setting").addEventListener("click", () => {
  dialog.showModal();
});

document.querySelector("button#reset").addEventListener("click", () => {
  localStorage.clear();
});

document.querySelector("button#applay").addEventListener("click", () => {
  localStorage.setItem("appid", appid.value);
  localStorage.setItem("token", token.value);
});

dialog.addEventListener("close", () => {
  setTimeout(updateCfg, 10);
});
